//
//  ViewController.m
//  testHttpSever
//
//  Created by mac on 16/5/25.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import "HTTPServer.h"
#import "MyHTTPConnection.h"
#import <ifaddrs.h>
#import <arpa/inet.h>

@interface ViewController ()
{
    HTTPServer *httpServer;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSString *temp1 = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil];
    NSRange indexRange = [temp1 rangeOfString:@"/index.html"];
    NSString *docRoot = [temp1 substringToIndex:indexRange.location];
    
    httpServer = [[HTTPServer alloc] init];
    __weak typeof(self) wself = self;
    [httpServer setHttpStartCallback:^(unsigned int port) {
        NSString *logStr = [NSString stringWithFormat:NSLocalizedString(@"请使用PC浏览器访问%@:%u，上传txt文本和封面",@""),[wself deviceIPAdress],port];
        NSLog(@"%@",logStr);
    }];
    
    //    char iphoneIP[200];
    
    // Tell the server to broadcast its presence via Bonjour.
    // This allows browsers such as Safari to automatically discover our service.
    [httpServer setType:@"_http._tcp."];
    
    [httpServer setDocumentRoot:docRoot];
    
    [httpServer setConnectionClass:[MyHTTPConnection class]];
    [httpServer setPort:52874];
    NSError *error = nil;
    if(![httpServer start:&error])
    {
        NSLog(@"Error starting HTTP Server: %@", error);
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"button" forState:UIControlStateNormal];
    button.frame = CGRectMake(50, 50, 50, 100);
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonClick {
    [self logAtest:@"123457745"];
}

- (void)logAtest:(NSString *)testStr {
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//    [btn setTitle:@"button" forState:UIControlStateNormal];
//    [btn setFrame:CGRectMake(50, 50, 100, 50)];
//    [self.view addSubview:btn];
    NSLog(@"git版本回退测试");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)deviceIPAdress {
    NSString *address = @"an error occurred when obtaining ip address";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    
    success = getifaddrs(&interfaces);
    
    if (success == 0) { // 0 表示获取成功
        
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    freeifaddrs(interfaces);
    
    return address;
}

@end
